home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / os2 / octa209s.zip / octave-2.09 / liboctave / pathsearch.cc < prev    next >
C/C++ Source or Header  |  1996-03-23  |  3KB  |  183 lines

  1. /*
  2.  
  3. Copyright (C) 1996 John W. Eaton
  4.  
  5. This file is part of Octave.
  6.  
  7. Octave is free software; you can redistribute it and/or modify it
  8. under the terms of the GNU General Public License as published by the
  9. Free Software Foundation; either version 2, or (at your option) any
  10. later version.
  11.  
  12. Octave is distributed in the hope that it will be useful, but WITHOUT
  13. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  15. for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with Octave; see the file COPYING.  If not, write to the Free
  19. Software Foundation, 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  20.  
  21. */
  22.  
  23. #ifdef HAVE_CONFIG_H
  24. #include <config.h>
  25. #endif
  26.  
  27. #include <cstdlib>
  28.  
  29. #include <string>
  30.  
  31. #include "pathsearch.h"
  32. #include "str-vec.h"
  33.  
  34. extern "C"
  35. {
  36. // Have to redefine these because they conflict with new C++ keywords
  37. // or otherwise cause trouble...
  38. #define boolean kpse_boolean
  39. #define true kpse_true
  40. #define false kpse_false
  41. #define string kpse_string
  42.  
  43. #include <kpathsea/pathsearch.h>
  44.  
  45. extern unsigned int kpathsea_debug;
  46.  
  47. #undef true
  48. #undef false
  49. #undef boolean
  50. #undef string
  51. }
  52.  
  53. bool dir_path::kpathsea_debug_initialized = false;
  54.  
  55. string_vector
  56. dir_path::elements (void)
  57. {
  58.   return initialized ? pv : string_vector ();
  59. }
  60.  
  61. string_vector
  62. dir_path::all_directories (void)
  63. {
  64.   int count = 0;
  65.   string_vector retval;
  66.  
  67.   if (initialized)
  68.     {
  69.       int len = pv.length ();
  70.  
  71.       int nmax = len > 32 ? len : 32;
  72.  
  73.       retval.resize (len);
  74.  
  75.       for (int i = 0; i < len; i++)
  76.     {
  77.       str_llist_type *elt_dirs = kpse_element_dirs (pv[i].c_str ());
  78.  
  79.       str_llist_elt_type *dir;
  80.       for (dir = *elt_dirs; dir; dir = STR_LLIST_NEXT (*dir))
  81.         {
  82.           char *elt_dir = STR_LLIST (*dir);
  83.  
  84.           if (elt_dir)
  85.         {
  86.           if (count == nmax)
  87.             nmax *= 2;
  88.  
  89.           retval.resize (nmax);
  90.  
  91.           retval[count++] = elt_dir;
  92.         }
  93.         }
  94.     }
  95.  
  96.       retval.resize (count);
  97.     }
  98.  
  99.   return retval;
  100. }
  101.  
  102. string
  103. dir_path::find_first (const string& nm)
  104. {
  105.   string retval;
  106.  
  107.   if (initialized)
  108.     {
  109.       char *tmp = kpse_path_search (p.c_str (), nm.c_str (),
  110.                     kpse_true);
  111.       if (tmp)
  112.     {
  113.       retval = tmp;
  114.       free (tmp);
  115.     }
  116.     }
  117.  
  118.   return retval;
  119. }
  120.  
  121. string_vector
  122. dir_path::find_all (const string& nm)
  123. {
  124.   string_vector retval;
  125.  
  126.   kpse_string *tmp = kpse_all_path_search (p.c_str (), nm.c_str ());
  127.  
  128.   if (tmp)
  129.     {
  130.       int count = 0;
  131.       kpse_string *ptr = tmp;
  132.       while (*ptr++)
  133.     count++;
  134.  
  135.       retval.resize (count);
  136.  
  137.       for (int i = 0; i < count; i++)
  138.     retval[i] = tmp[i];
  139.     }
  140.  
  141.   return retval;
  142. }
  143.  
  144. void
  145. dir_path::init (void)
  146. {
  147.   initialized = false;
  148.  
  149.   if (! kpathsea_debug_initialized)
  150.     {
  151.       char *s = getenv ("KPATHSEA_DEBUG");
  152.  
  153.       if (s)
  154.     kpathsea_debug |= atoi (s);
  155.     }
  156.  
  157.   int count = 0;
  158.   char *path_elt = kpse_path_element (p.c_str ());
  159.   while (path_elt)
  160.     {
  161.       path_elt = kpse_path_element (0);
  162.       count++;
  163.     }
  164.  
  165.   pv.resize (count);
  166.  
  167.   path_elt = kpse_path_element (p.c_str ());
  168.  
  169.   for (int i = 0; i < count; i++)
  170.     {
  171.       pv[i] = path_elt;
  172.       path_elt = kpse_path_element (0);
  173.     }
  174.  
  175.   initialized = true;
  176. }
  177.  
  178. /*
  179. ;;; Local Variables: ***
  180. ;;; mode: C++ ***
  181. ;;; End: ***
  182. */
  183.